import pymysql
#host数据库服务器地址
#默认端口3306
dbParmas = {
    'host': '127.0.0.1',
    'user': 'root',
    'password': '199802',
    'db': 'xuesheng',
    'port':3306,
    'charset':'utf8'
}
conn = pymysql.Connect(**dbParmas) #任意关键字参数
# conn = pymysql.Connect(host='127.0.0.1', user='root', password='199802',db='xuesheng', port=3306,charset='utf8')
print(conn)
cursor = conn.cursor()
try:#对增删改 操作 pymysql 默认begin 事务,必须提交才可以 或者回滚rollback
    sno = input('学号')
    sname = input('姓名')
    ssex = input('性别')
    sql = "insert into student(sno,sname,ssex) values('{}','{}','{}')".format(sno,sname,ssex)
    print(sql)
    # exit()#调试看前面的代码正确吗
    result = cursor.execute(sql)
    conn.commit()#提交
    print(result)
    if result > 0:
        print('执行成功')
    else:
        print('失败')
except Exception as e:
    print(e)
    conn.rollback()#回滚
finally:#插入失败后最后关闭游标和连接
    cursor.close()
    conn.close()
    exit()#退出程序

cursor.close()
conn.close()

